export function generateUUID() {
  var d = new Date().getTime();
  if (window.performance && typeof window.performance.now === "function") {
    d += performance.now(); //use high-precision timer if available
  }
  var uuid = "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function(
    c
  ) {
    var r = (d + Math.random() * 16) % 16 | 0; //单竖杠“|”就是转换为2进制之后相加得到的结果
    d = Math.floor(d / 16);
    return (c == "x" ? r : (r & 0x3) | 0x8).toString(16);
  });
  return uuid;
}

export function randomeId() {
  const num1 = new Date()
    .getTime()
    .toString()
    .slice(-8);
  const num2 = Math.floor(Math.random() * 8 * 10).toString();
  const id = Number(num1 + num2);
  return id;
}


export function formatDate(date) {
  if (!date || !(date instanceof Date)) {
    date = new Date(date);
  }
  const year = date.getFullYear();
  const month = date.getMonth() + 1;
  const day = date.getDate();
  const hour = date.getHours();
  const minute = date.getMinutes();
  const second = date.getSeconds();
  return `${year}-${month}-${day} ${hour}:${minute}:${second}`;
}


/* 类型判断 */
export function checkType(data) { // 用来判断被拷贝对象数据类型的函数
	return Object.prototype.toString.call(data).slice(8, -1)
}
/**
 * @param {any} source
 * @returns {Object}
 */

export function deepClone(source) { // 实现深度拷贝的函数
	let result, sourceType = checkType(source)
	if (sourceType === 'Object') { // 只有对象和数组才进行深度拷贝
		result = {}
	} else if (sourceType === 'Array') {
		result = []
	} else {
		return source
	}
	for (let i in source) { // i为对象的key或数组的下标

      let value = source[i]
      let valueType = checkType(i)
      if (valueType === 'Object' || valueType === 'Array') {
        // 当拷贝的对象中还有数组或是对象时进行递归，克隆它们中的内容，直到递归完所有数组或对象为止
        result[i] = deepClone(value)
      } else {
        result[i] = value
      }
	}
	return result // 返回最终的结果
}

/**
 * @param {Object} FirstOBJ,SecondOBJ
 * @returns {Object}
 */
export function deepAssign(FirstOBJ, SecondOBJ) { // 深度合并对象
	for (var key in SecondOBJ) {
		FirstOBJ[key] = FirstOBJ[key] && FirstOBJ[key].toString() === "[object Object]" ?
			deepAssign(FirstOBJ[key], SecondOBJ[key]) : FirstOBJ[key] = SecondOBJ[key];
	}
	return FirstOBJ;
}

// 统计在线时长
export function timeLong(start,end){
  const hour = 1000*60*60
  const minute = 1000*60
  const rang = end - start
  let h = 0
  let m = 0
  if(rang > hour){
    h =    parseInt(rang/hour) 
    let  residue =  rang - h*hour
    if(residue > minute){
      m = parseInt(residue/minute)
    } 
  }else if(rang > minute){
      m = parseInt(rang/minute)
  }
return h > 0 ? `${h}小时${m}分钟`:`${m}分钟`
}


// 高位补0 
export function addZero(num,length){
      return (Array(length).join('0')+num).slice(-length)
}